package com.ptkc.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

public class MyUtil {

	private static final double EARTH_RADIUS = 6371000;
	private static final String[] chars = { "0", "1", "2", "3", "4", "5", "6","7",
			"8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J","K", "L","M", "N","O",
			"P", "Q", "R", "S", "T", "U","V", "W", "X", "Y", "Z", "a", "b", "c", "d",
			"e", "f", "g", "h", "i", "j", "k", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
			"w", "x", "y", "z" };

	private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
	private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
	private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
	private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
	private static final String ak2 = "BrbDrmC3GyIBRExpp5jXF0HpcXcP9P5v";//百度AK
	//    private static final String ak = "jxL9ZCE1KGypzG1MbPwSSAcR";//百度AK
	private static final String ak = "BrbDrmC3GyIBRExpp5jXF0HpcXcP9P5v";//百度AK

	private static final String gaoDekey = "f2d9f1f42e56a8e5ddf9b3a1b669f3b9";
	//final String gaoDe


	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm");
	/**
	 * 设置时间为该天的结束时间：yyyy-MM-dd 23:59:59
	 * */
	public static Date getEndDay(Date time){

		Calendar cl = Calendar.getInstance();
		cl.setTime(time);
		cl.set(Calendar.HOUR_OF_DAY, 23);
		cl.set(Calendar.MINUTE, 59);
		cl.set(Calendar.SECOND, 59);
		cl.set(Calendar.MILLISECOND, 999);
		return cl.getTime();
	}
	/**
	 * 设置时间为该天的开始时间：yyyy-MM-dd 00:00:00
	 * */
	public static  Date getStartDay(Date time){

		Calendar cl = Calendar.getInstance();
		cl.setTime(time);
		cl.set(Calendar.HOUR_OF_DAY, 0);
		cl.set(Calendar.MINUTE, 0);
		cl.set(Calendar.SECOND, 0);
		cl.set(Calendar.MILLISECOND, 0);
		return cl.getTime();
	}
	/**
	 * 找出给出时间的前几天记录
	 * @param time
	 * @param beforeDay 前？天
	 * @return
	 */
	public static Date getBeforeDay(Date time,Integer beforeDay) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(time);
		cl.set(Calendar.HOUR_OF_DAY, 0);
		cl.set(Calendar.MINUTE, 0);
		cl.set(Calendar.SECOND, 0);
		cl.add(Calendar.DAY_OF_MONTH, beforeDay);
		return cl.getTime();

	}
	/**
	 * 获取给出时间的当月第一天
	 * @param time
	 * @return
	 */
	public static Date getMonthStartDate(Date time){
		Calendar cl = Calendar.getInstance();
		cl.setTime(time);
		cl.set(Calendar.DAY_OF_MONTH, 1);
		return getStartDay(cl.getTime());
	}
	/**
	 * 获取给出时间的当月最后一天
	 * @param time
	 * @return
	 */
	public static Date getMonthEndDate(Date time){
		Calendar cl = Calendar.getInstance();
		cl.setTime(time);
		cl.add(Calendar.MONTH, 1);
		cl.set(Calendar.DAY_OF_MONTH, 0);
		return getEndDay(cl.getTime());
	}


	/**
	 * 获取当前时间的当月第一天
	 * @param //time
	 * @return
	 */
	public static Date getnowMonthStartDate(){
		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.DAY_OF_MONTH, 1);
		return getStartDay(cl.getTime());
	}
	/**
	 * 获取给出时间的当月最后一天
	 * @param //time
	 * @return
	 */
	public static Date getnowMonthEendDate(){
		Calendar cl = Calendar.getInstance();
		cl.add(Calendar.MONTH, 1);
		cl.set(Calendar.DAY_OF_MONTH, 0);
		return getEndDay(cl.getTime());
	}
	/**
	 * 隐藏电话号码中间4位
	 * @param phone
	 * */
	public static String ellipsizePhone(String phone){
		String newPhone = phone;
		if(phone!=null&&phone.length()>=11){//13768273510
			newPhone = phone.substring(0,3)+"****"+phone.substring(7);
		}
		return newPhone;
	}
	/**
	 * 获取随机字符
	 * */
	public static String randomChar(int size){
		StringBuffer sb = null;
		if(size>0){
			sb = new StringBuffer();
			Random ran = new Random();
			for(int i = 1; i <= size; i++ ){
				int r = ran.nextInt(chars.length);
				sb.append(chars[r]);
			}
		}
		return sb.toString();
	}
	/**
	 * 获取随机数
	 * */
	public static String randomNumber(int size){
		StringBuffer sb = null;
		if(size>0){
			sb = new StringBuffer();
			Random ran = new Random();
			for(int i = 1; i <= size; i++ ){
				int r = ran.nextInt(10);
				sb.append(r);
			}
		}
		return sb.toString();
	}

	/**
	 * 当前时间加一段时间
	 * @return
	 */
	public static Date addTimeSpan(Integer month,Integer day) {
		Calendar calendar = Calendar.getInstance();
		if(month != null) {
			calendar.add(Calendar.MONTH, month);
		}
		if(day != null) {
			calendar.add(Calendar.DAY_OF_MONTH, day);
		}
		return calendar.getTime();
	}

	/**
	 * 给定时间加一段时间
	 * @return
	 */
	public static Date addTimeSpan(Date date,Integer month,Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		calendar.add(Calendar.DAY_OF_MONTH, day);
		return calendar.getTime();
	}

	/**
	 * 判断两个时间是否是同一天
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDay(Date date1, Date date2) {
		Calendar calDateA = Calendar.getInstance();
		calDateA.setTime(date1);

		Calendar calDateB = Calendar.getInstance();
		calDateB.setTime(date2);

		return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
				&& calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
				&& calDateA.get(Calendar.DAY_OF_MONTH) == calDateB
				.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 判断两个时间是否是同一天
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDay(long date1, long date2) {
		Calendar calDateA = Calendar.getInstance();
		calDateA.setTimeInMillis(date1);

		Calendar calDateB = Calendar.getInstance();
		calDateB.setTimeInMillis(date2);

		return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
				&& calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
				&& calDateA.get(Calendar.DAY_OF_MONTH) == calDateB
				.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取小时
	 * @param date
	 * @return
	 */
	public static int getHourOfDay(Date date){
		Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
		c.setTime(date);
		return c.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获取当前时间在今天的分钟数
	 * @return
	 */
	public static int getMinuteOfToDay(){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		return hour * 60 + minute;
	}

	/**
	 * 获取天
	 * @param date
	 * @return
	 */
	public static int getDayOfMonth(Date date){
		Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
		c.setTime(date);
		return c.get(Calendar.DAY_OF_MONTH);
	}

	public static int getDayOfWeek(Date date){
		Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
		c.setTime(date);
		int d = (c.get(Calendar.DAY_OF_WEEK) - 1 );
		if(d == 0){
			d = 7;
		}
		return d;
	}




	public static Date set(Long date,Integer year, Integer month, Integer day){
		GregorianCalendar c = new GregorianCalendar();//可以对每个时间域单独修改
		c.setTimeInMillis(date);
		if(year != null){
			c.set(Calendar.YEAR, year);
		}
		if(month != null){
			c.set(Calendar.MONTH, month);
		}
		if(day != null){
			c.set(Calendar.DAY_OF_MONTH, day);
		}
		return c.getTime();
	}

	public static Date set(Date date,int val, int field){
		//可以对每个时间域单独修改
		GregorianCalendar c = new GregorianCalendar();
		c.setTime(date);
		c.set(field, val);
		return c.getTime();
	}

	/**
	 * 时间 date 是否在date1 ，date2之间
	 * @param date
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public static boolean inTime(Date beginTime,Date endTime,Date date){

		boolean after = false;
		Calendar curDate = Calendar.getInstance();
		curDate.setTime(date);
		long d=curDate.getTimeInMillis();
		//int hour=curDate.get(Calendar.HOUR_OF_DAY);
		Calendar cal=Calendar.getInstance();

		cal.setTime(endTime);

		int endour=cal.get(Calendar.HOUR_OF_DAY);
		int min=cal.get(Calendar.MINUTE);
		curDate.set(Calendar.HOUR_OF_DAY, endour);
		curDate.set(Calendar.MINUTE, min);
		long d2=curDate.getTimeInMillis();

		cal.setTime(beginTime);
		// hour=cal.get(Calendar.HOUR_OF_DAY);
		int beginour=cal.get(Calendar.HOUR_OF_DAY);
		min=cal.get(Calendar.MINUTE);
		curDate.set(Calendar.HOUR_OF_DAY, beginour);
		curDate.set(Calendar.MINUTE, min);
		long d1=curDate.getTimeInMillis();

		if(d>=d1&&d<=d2){
			after = true;
		}

		if(beginTime.after(endTime)){
			if(d>=d1||d<=d2){
				after = true;
			}
		}
		return after;
	}
}
